package com.longg.controller.article;

import com.longg.comm.Comm;
import com.longg.controller.BaseController;
import com.longg.model.Article;
import com.longg.service.IArticleService;
import com.longg.service.impl.ArticleServiceImpl;
import com.longg.utils.PageUtil;

import javax.mail.Session;
import javax.servlet.DispatcherType;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@WebServlet("/article/*")
public class ArticleController extends BaseController {
    private IArticleService articleService = new ArticleServiceImpl();

    public void findArticleByPage(HttpServletRequest req, HttpServletResponse resp){
        int currentPage = 1;
        HttpSession session = req.getSession();
        String page = req.getParameter("currentPage");
        String name = req.getParameter("name");
        if(null != page && !"".equals(page)){
            currentPage = Integer.parseInt(page);
        }
        List<Map<String, Object>> articleByPage = articleService.findArticleByPage(currentPage,name);
        long count = articleService.getCount(name);
        long totalPage = PageUtil.getTotalPage(count);
        long startPage = PageUtil.getStartPage(currentPage);
        long endPage = PageUtil.getEndPage(currentPage,count);
        session.setAttribute("currentPage",currentPage);
        session.setAttribute("count",count);
        session.setAttribute("totalPage",totalPage);
        session.setAttribute("startPage",startPage);
        session.setAttribute("endPage",endPage);
        session.setAttribute("articleByPage",articleByPage);
        session.setAttribute("pageCount", Comm.PAGE_COUNT);
        session.setAttribute("name",name);
        try {
            resp.sendRedirect(req.getContextPath() + "/assets/html/article.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void favouriteArticle(HttpServletRequest req, HttpServletResponse resp){
        int currentPage = 1;
        HttpSession session = req.getSession();
        int id = getUserId(req);
        String page = req.getParameter("currentPage");
        String name = req.getParameter("name");
        if(null != page && !"".equals(page)){
            currentPage = Integer.parseInt(page);
        }
        List<Map<String, Object>> favouriteArticle = articleService.favouriteArticle(id,currentPage,name);
        long count = articleService.getFCount(id,name);
        long totalPage = PageUtil.getTotalPage(count);
        long startPage = PageUtil.getStartPage(currentPage);
        long endPage = PageUtil.getEndPage(currentPage,count);
        session.setAttribute("currentPage",currentPage);
        session.setAttribute("count",count);
        session.setAttribute("totalPage",totalPage);
        session.setAttribute("startPage",startPage);
        session.setAttribute("endPage",endPage);
        session.setAttribute("favouriteArticle",favouriteArticle);
        session.setAttribute("pageCount", Comm.PAGE_COUNT);
        session.setAttribute("name",name);
        try {
            resp.sendRedirect(req.getContextPath() + "/assets/html/article_collect.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addArticle(HttpServletRequest req, HttpServletResponse resp){
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        HttpSession session = req.getSession();
        int id = getUserId(req);
        String uName = getUserName(req);
        try {
            if(null == title || "".equals(title) || null == content || "".equals(content)){
                session.setAttribute("aMsg","标题或内容不能为空！");
                resp.sendRedirect(req.getContextPath() + "/assets/html/article_add.jsp");
                return;
            }else{
                boolean flag = articleService.addArticleticle(uName,id,title,content);
                if(flag){
                    session.setAttribute("aMsg","发表成功！");
                    resp.sendRedirect(req.getContextPath() + "/article/findArticleByPage");
                }else{
                    session.setAttribute("aMsg","发表失败！");
                    resp.sendRedirect(req.getContextPath() + "/assets/html/article_add.jsp");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void articleDetail(HttpServletRequest req, HttpServletResponse resp){
        String aidStr = req.getParameter("aid");
        HttpSession session = req.getSession();
        if (null == aidStr || "".equals(aidStr)){
            aidStr = "0";
        }
        int aid = Integer.parseInt(aidStr);
        Map<String, Object> articleByid = articleService.getArticleByid(aid);
        long fCount = articleService.getFCount(aid, null);
        articleByid.put("fCount",fCount);
        session.setAttribute("articleDetail",articleByid);
        try {
            resp.sendRedirect(req.getContextPath() + "/assets/html/article_detail.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void offFavorite(HttpServletRequest req, HttpServletResponse resp){
        String aid = req.getParameter("aid");
        int userId = getUserId(req);
        try {
            if(null == aid || "".equals(aid)){
                resp.getWriter().write("参数不对！");
                return;
            }
            boolean flag = articleService.offFavorite(Integer.parseInt(aid),userId);
            if(flag){
                resp.getWriter().write("1");
            }else{
                resp.getWriter().write("操作频繁！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void onFavorite(HttpServletRequest req, HttpServletResponse resp){
        String aidStr = req.getParameter("aid");
        int uid = getUserId(req);
        try {
            if(null == aidStr || "".equals(aidStr)){
                resp.getWriter().write("参数不对");
                return;
            }
            int aid = Integer.parseInt(aidStr);
            boolean ifFav = articleService.ifFavorite(uid,aid);
            boolean flag = articleService.onFavorite(uid, aid);
            if(ifFav){
                resp.getWriter().write("操作频繁！");
            }else{
                if(flag){
                    resp.getWriter().write("1");
                }else{
                    resp.getWriter().write("收藏失败");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
